package com.xiaotao.service.impl;

import com.xiaotao.entity.User;
import com.xiaotao.entity.UserSign;
import com.xiaotao.entity.Wallet;
import com.xiaotao.mapper.UserMapper;
import com.xiaotao.mapper.UserSignMapper;
import com.xiaotao.mapper.WalletMapper;
import com.xiaotao.service.UserSignService;
import com.xiaotao.util.ResponseEnum;
import com.xiaotao.util.ResponseUtil;
import com.xiaotao.util.UserUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class UserSignServiceImpl implements UserSignService {
    @Resource
    private UserSignMapper userSignMapper;
    @Resource
    private WalletMapper walletMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private RedisTemplate redisTemplateInit;

    @Override
    public ResponseUtil add(UserSign userSign) throws Exception {
        User user = new User();
        Integer userId = UserUtil.UserContext();
        userSign.setUserId(userId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(new Date());
        String yesterday = sdf.format(new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000));
        String todaySign = (String) redisTemplateInit.opsForHash().get("sign:"+"sign" + userId, today);
        if (todaySign != null) {
            return ResponseUtil.get(ResponseEnum.FAIL, "今日已签到，无需重复操作");
        }
        String yesterdaySign = (String) redisTemplateInit.opsForHash().get("sign:"+"sign" + userId, yesterday);
        if (yesterdaySign == null) {
            user.setId(userId);
            user.setSignDays(1);
            userMapper.updateSignDays(user);
        } else {
            User user1 = userMapper.queryById(userId);
            user1.setSignDays(user1.getSignDays() + 1);
            userMapper.updateSignDays(user1);
        }
        User user2 = userMapper.queryById(userId);
        Integer signDays = user2.getSignDays();
        Wallet wallet = walletMapper.queryByUserId(userId);
        if (signDays >= 1 && signDays <= 6) {
            wallet.setPoints(wallet.getPoints() + 30);
            walletMapper.addPoint(wallet);
        } else if (signDays >= 7 && signDays <= 14) {
            wallet.setPoints(wallet.getPoints() + 50);
            walletMapper.addPoint(wallet);
        } else if (signDays >= 15 && signDays <= 30) {
            wallet.setPoints(wallet.getPoints() + 80);
            walletMapper.addPoint(wallet);
        } else if (signDays >= 31) {
            wallet.setPoints(wallet.getPoints() + 100);
            walletMapper.addPoint(wallet);
        }
        redisTemplateInit.opsForHash().put("sign:"+"sign" + userId,today,today);
        return ResponseUtil.get(ResponseEnum.OK, "签到成功！连续签到" + signDays + "天，获得" + (signDays <= 6 ? 30 : (signDays <= 14 ? 50 : (signDays <= 30 ? 80 : 100))) + "积分");
    }
    @Override
    public ResponseUtil queryByIdAndDate(Integer userId, Date date) throws Exception {
        userId = UserUtil.UserContext();
        UserSign userSign = userSignMapper.queryByIdAndDate(userId, date);
        return userSign != null ? ResponseUtil.get(ResponseEnum.OK) :
                ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil queryAll(Integer userId) throws Exception {
        List<String> userSigns = new ArrayList<>();
        Map entries = redisTemplateInit.opsForHash().entries("sign:"+"sign" + userId);
        if (!entries.isEmpty()&&entries!=null){
            Collection values = entries.values();
            userSigns.addAll(values);
        }
        return ResponseUtil.get(ResponseEnum.OK,userSigns);
    }

    @Override
    public ResponseUtil queryByUserId(Integer id) {
        User user = userSignMapper.queryByUserId(id);
        Integer signDays = user.getSignDays();
        return user != null ? ResponseUtil.get(ResponseEnum.OK, signDays) :
                ResponseUtil.get(ResponseEnum.FAIL);
    }
}
